<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html class="win-frame">
<head>
<title>${name_china}管理-编辑${name_china}</title>
<%@include file="/common/head.jsp" %>
<style>
.maskback{
	position:fixed;
	width:100%;
	height:100%;
	top:0px;
	background:rgba(0,0,0,0.7);
	z-index: 999;
}
.mainback{
    width: 40%;
    height: 450px;
    background: #fff;
    margin: 0 auto;
    margin-top: 4%;
    overflow: auto;
}
</style>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
            <span class="split"></span>
            <span>编辑${name_china}</span>
        </div>
        <a id="goBack" class="btn-return distance-left" href="javascript:;"><i class="icon icon-arrow-left"></i>返回上级</a>
    </div>
    <div class="page-title-border"></div>
</div>

<form id="form" method="post">
    <div class="form-wrapper">
	  <div class="h4 distance-double">
        <span class="h4-title"><i class="icon icon-setting"></i>基本信息</span>
        <span class="h4-border"></span>
      </div>
      
      <input type="hidden" id="id" name="id"/> 
      <input type="hidden" id="spuIds" name="spuIds"/> 
	     <div class="form-cont">
	        <p class="form-cont-title">平台</p>
	        <input type="text" id="spuPlatform" name="spuPlatform" disabled class="summer-input summer-validatebox"  /> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title">特卖时间</p>
	        <input type="text" id="specialTime" name="specialTime" class="summer-input summer-validatebox" /> 
	     </div>
	     
	     <div class="form-cont">
	        <p class="form-cont-title">特卖结束时间</p>
	        <input type="text" id="specialEndTime" name="specialEndTime" class="summer-input summer-validatebox" /> 
	     </div>
	     
     <div class="form-btn-wrapper" id="funcbar">
        <a class="btn btn-normal" id="backBtn">关闭</a>
    </div>
  </div>
</form>
<div class="search-field">
	<span class="field-cont">
        <span class="field-cont-title">商品名称</span>
        <input type="text" class="summer-input" id="goodsName"/>
    </span>
    
     <span class="field-cont">
        <span class="field-cont-title">商品分类</span>
        <input type="text" id="categoryId" name="categoryId" class="summer-input summer-validatebox"  /> 
    </span><br/>
   
     <span class="field-cont">
        <span class="field-cont-title">是否爆款</span>
        <input type="text" class="summer-input" id="isHot"/>
    </span><br/>
    <span class="field-btn-wrapper">
        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
    </span>
</div>

<div style="display: none" class="maskback">
     <div class="mainback">
     <div class="form-cont" style="top:40px;">
     <input type="hidden" name="spuId" id="spuId" />
     <p class="form-cont-title">商品原价</p>
     <input type="text" id="price" name="price"  class="summer-input summer-validatebox" disabled/> 
     </div>
     <div class="form-cont" style="top:40px;">
     <p class="form-cont-title">券后价</p>
     <input type="text" id="conPrice" name="conPrice" class="summer-input summer-validatebox" disabled/> 
     </div>
<!--      <div id="isShow">
 -->     <div class="form-cont" style="top:40px;">
     <p class="form-cont-title"><span class="field-required">*</span>红包券金额</p>
     <input type="text" id="couponMoney" name="couponMoney" class="summer-input summer-validatebox" />
     </div>
     <div class="form-cont" style="top:40px;">
     <p class="form-cont-title"><span class="field-required">*</span>数量</p>
     <input type="text" id="maxCount" name="maxCount" class="summer-input summer-validatebox" />
     </div>
     <div class="form-cont" style="top:40px;">
     <p class="form-cont-title"><span class="field-required">*</span>佣金</p>
     <input type="text" id="wlCommission" name="wlCommission" class="summer-input summer-validatebox" />
     </div>
     <!-- </div> -->
	     <div class="form-btn-wrapper" id="funcbar" style="margin-top: 100px;">
	        <a id="save" href="javascript:;" class="btn btn-ok btn-normal  wave distance-right" >保存</a>
	        <a class="btn btn-normal" id="backBtnMark">关闭</a>
	     </div>
     </div>
</div>

<div id="t-wrap">
    <table class="summer-datagrid" id="dataList">
	    <col width="10%">
	    <col width="10%">
	    <col width="30%">
	    <col width="20%">
	    <col width="20%">
	    <col width="20%">
	    <col width="20%">
	    <col width="25%">	
	    <col width="25%">
	    <thead>
		     <tr>
		    <th><div class="summer-datagrid-cell"></div></th>
		    <th><div class="summer-datagrid-cell">排序</div></th>
		    <th><div class="summer-datagrid-cell">名称</div></th>
		    <th><div class="summer-datagrid-cell">图片</div></th>
		    <th><div class="summer-datagrid-cell">类型</div></th>
		    <th><div class="summer-datagrid-cell">分类</div></th>
		    <th><div class="summer-datagrid-cell">价钱（原价、券后价、佣金数）</div></th>
		    <th><div class="summer-datagrid-cell">时间</div></th>
		    <th><div class="summer-datagrid-cell">操作</div></th>
			 </tr>
	    </thead>
    </table>
    
</div>
<script>
    
  var   editData,
  	 	permission = CommonUtil.getPermission('${name_english}');
    $(function () {
    	editData = ${editData};
        console.log(editData);
    	 $('#form').form({
         	onLoadSuccess:function(data){
                
            }
         }).form('loadDataEdit', {
             data: editData
         });
    	$("#id").val(${id});
    	$("#spuIds").val(${spuIds});
     	 //下拉初始化
     	  /*    $('#spuPlatform').combobox({
     	    	async:false,
     	        defaultValue: editData.spuPlatform,
              valueField : 'code',
              textField : 'name',
              url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_SPU_PLATFORM'
          });  */
      	 $('#isHot').combobox({
               valueField : 'code',
               textField : 'name',
               url : COMMON_URL.getCode + '?type=T_DM_T_GOODS_SPU_IS_HOT'
           });
      	//初始化下拉选项  商品所属公司
      	 $('#categoryId').combobox({
               url : CONST_URL.GET_GOODS_CATEGORY, 
               valueField : 'id',
               async:false,
               textField : 'name'
           });
      	//初始化下拉选项  商品所属公司
      	  	         console.log(editData.specialTime);
    	 $('#specialTime').combobox({
    		 async:false,
    		 defaultValue: editData.specialTime,
    		 url : CONST_URL.GET_TIME, 
             valueField : 'startTime',
             textField : 'startTime'
         }); 
        //$("#specialTime").val(editData.specialTime);
    	
    	 $('#specialEndTime').combobox({
    		 async:false,
    		 defaultValue: editData.specialEndTime,
    		 url : CONST_URL.GET_TIME, 
             valueField : 'endTime',
             textField : 'endTime'
         }); 
       //$("#specialEndTime").val(editData.specialEndTime);
    	//初始化funcbar
     	$('#funcbar').prepend($.funcbar({
            type: 'button',
            data: permission,
            permit: ['${name_english}:edit']
        }));
    	//商品编辑
     	$('#t-wrap').on('click','a[data-permit="${name_english}:initEditGoods"]',function() {
 			var $this = $(this), 
 			spuId = $this.closest('tr').find('td[data-field="spuId"]').attr('data-value');
 			conPrice = $this.closest('tr').find('td[data-field="conPrice"]').attr('data-value');
 			price = $this.closest('tr').find('td[data-field="wlPrice"]').attr('data-value');
 			maxCount = $this.closest('tr').find('td[data-field="maxCount"]').attr('data-value');
 			couponMoney = $this.closest('tr').find('td[data-field="couponMoney"]').attr('data-value');
 			wlCommission = $this.closest('tr').find('td[data-field="wlCommission"]').attr('data-value');
 			$("#maxCount").val(maxCount);
 			$("#price").val(price);
 			$("#conPrice").val(conPrice);
 			$("#couponMoney").val(couponMoney);
 			$("#wlCommission").val(wlCommission);
 			$("#spuId").val(spuId);
 			$(".maskback").show();     	
 			});
    	
    	$("#backBtnMark").on('click',function(){
    		$(".maskback").hide();
         	window.location.href = CONST_URL.INIT_EDIT+"?id="+$("#id").val();	
    	});
     	//商品信息编辑保存
     	$("#save").on('click',function(){
     		$(".maskback").hide();
     		var $this = $(this),
     		spuId = $("#spuId").val();
     		maxCount = $("#maxCount").val();
     		$.sdialog({
			type: 'confirm',
			msg: '您确定要保存该记录吗?',
			onConfirm: function(result) {
			    if (result) {
			    	$.sajax({
		                url : CONST_URL.EDIT_GOODS,
		                data: {
		                	"spuId": trimSpace($('#spuId').val()),
		                	"couponMoney": trimSpace($('#couponMoney').val()),
		    				"wlCommission": trimSpace($('#wlCommission').val()),
		    				"maxCount": trimSpace($('#maxCount').val())
		            	},
		                onLoadSuccess : function(data) {
		                	console.log(data);
		                	if(data.msg != "null"){
		                		alert(data.msg);
		                	}else{
		                		$.stip({
	            	                type:'ok',//显示类型 有info、 ok、 danger
	            	                text: '保存成功！'    
			                	});
			                	reloadTable();  
		                	}
		                }
		            });
			    		
			    }	
			}
		});
     	});
     	
     	$('#t-wrap').on('click','a[data-permit="${name_english}:delGoods"]',function() {
 			var $this = $(this), 
 			spuId = $this.closest('tr').find('td[data-field="spuId"]').attr('data-value');
     		$.sdialog({
    			type: 'confirm',
    			msg: '您确定要删除该商品吗?',
    			onConfirm: function(result) {
    			    if (result) {
    			    	$.sajax({
    		                url : CONST_URL.DEL_GOODS,
    		                data: {
    		                	"spuId": spuId,
    		                	"id": trimSpace($('#id').val())
    		            	},
    		                onLoadSuccess : function() {
    		                	$.stip({
	            	                type:'ok',//显示类型 有info、 ok、 danger
	            	                text: '删除成功！'    
			                	});
			                	reloadTable();  
    		                }
    		            });
    			    		
    			    }	
    			}
    		});
     	});
    	//保存按钮
    	$('#funcbar').on('click', 'a[data-permit="${name_english}:edit"]', submit);
     	 createTable();
    	//返回按钮
    	$('#backBtn, #goBack').on('click',backMain);
    	$('#reset').on('click', function() {
			//重置文本框的值
			$('#spuPlatform').combobox('clear');
			reloadTable();
		});
    });
    
    //表单提交
    function submit() {
         $("#form").form({
        	 buttons : ['#saveBtn'],
             ajax: {
                 url: CONST_URL.EDIT,
                 data: {
	                	"id": trimSpace($('#id').val()),
	                	"specialTime":$('#specialTime').combobox('getValue'),
    					"specialEndTime":$('#specialEndTime').combobox('getValue')
	            	},
             },
             onSubmitSuccess: function (data) {
            	 $.stip({
   	                type:'ok',//显示类型 有info、 ok、 danger
   	                text: '保存成功！',
   	                onBeforeHide: function () {
   	                	backMain();
   	                }
            	 });
              }
         }).form("submit");
    }
    
    //返回主页面
    function backMain() {
    	window.location.href = CONST_URL.INIT_MAIN;
    }
    // 重新加载表格
	function reloadTable() {
		$('#dataList').datagrid('reload',{
            ajax: {
            	param: {
            		"goodsName": trimSpace($('#goodsName').val()),
					"categoryId":$('#categoryId').combobox('getValue'),
					"isHot":$('#isHot').combobox('getValue'),
					"spuIds": trimSpace($('#spuIds').val())
            	}
            }
        });
	}
    
  //创建表格
	function createTable() {
		$('#dataList')
				.datagrid(
						{
							ajax : {
								url : CONST_URL.GOODS_LIST,
								param : {	
									"goodsName": trimSpace($('#goodsName').val()),
									"categoryId":$('#categoryId').combobox('getValue'),
									"isHot":$('#isHot').combobox('getValue'),
									"spuIds": trimSpace($('#spuIds').val())
								},
								success : function(data) { //成功 
									alert("Data Saved: " + msg);
								}
							},
							columns : [
									{
										field : 'spuId',
										hidden : true
									},
									{
										field: 'couponMoney',
										hidden: true
									},
									{
										field: 'wlCommission',
										hidden: true
									},
									{
										field: 'wlPrice',
										hidden: true
									},
									{
										field: 'conPrice',
										hidden: true
									},
									{
										field: 'maxCount',
										hidden: true
									},
									{
										render : function(rowData){
											return '<input type="checkbox" id="spuId" name="spuId" value="'+rowData.spuId+'">';
										}
									},
									{
										field : 'reorder'
									},
									{
										field : 'goodsName'
									},
									{
										render : function(rowData){
											return '<img width="100" height="100" src="'+rowData.mainImageUrl+'">';
										}
									},
									{
										field : 'spuPlatform'
									},
									{
										field : 'categoryId'
									},
									{
										render : function(rowData){
											return '<p>原价：'+rowData.wlPrice+'</p><br/>'
												  +'<p>券后价：'+rowData.conPrice+'</p><br/>'
												  +'<p>佣金：'+rowData.wlCommission+'</p>';
										}
									},
									{
										field : 'createTime'
									},
									{
										render : function(rowData) {
											if(rowData.spuPlatform == "友惠家"){
						                        return $.funcbar({
						                          data : permission, //请求的数据
						                          type : 'text', //类型
						                          display : 2, //显示数量
						                          permit : [
						                              '${name_english}:initEditGoods','${name_english}:delGoods'
						                               ]
						                        //需要的权限按钮
						                        });
						                      }else{
						                    	  return $.funcbar({
							                          data : permission, //请求的数据
							                          type : 'text', //类型
							                          display : 1, //显示数量
							                          permit : [
							                              '${name_english}:delGoods'
							                               ]
							                        //需要的权限按钮
							                        });  
						                      }
										}
									}

							]
						});
	}
    //请求url
    var CONST_URL = {
    	INIT_MAIN: basePath + "${common_url}init",
       	EDIT: basePath + "${common_url}edit",
       	GET_GOODS_CATEGORY: basePath + "${common_url}getGoodsCategory",
       	GOODS_LIST: basePath + "${common_url}goodsList",
       	EDIT_GOODS: basePath + "${common_url}editGoods",
       	INIT_EDIT_GOODS: basePath + "${common_url}initEditGoods",
       	INIT_EDIT: basePath + "${common_url}initEdit",
       	EDIT_GOODS: basePath + "${common_url}editGoods",
       	DEL_GOODS: basePath + "${common_url}delGoods",
       	GET_TIME: basePath + "${common_url}getTime"
    };
    
</script>
</body>
</html>